Total Complexity | 2 |
Total Lines | 23 |
Duplicated Lines | 0 % |
Coverage | 100% |
Changes | 0 |
1 | 7 | import { Injectable, UnauthorizedException } from '@nestjs/common'; |
|
7 | |||
8 | @Injectable() |
||
9 | 7 | export class JwtStrategy extends PassportStrategy(Strategy) { |
|
10 | constructor( |
||
11 | 5 | private configService: ConfigService, |
|
12 | 5 | private authService: AuthService, |
|
13 | ) { |
||
14 | // const secret = process.env.JWT_SECRET || 'your-secret-key'; |
||
15 | // console.log('JwtStrategy initialized with secret:', secret); |
||
16 | 5 | super({ |
|
17 | jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), |
||
18 | ignoreExpiration: false, |
||
19 | secretOrKey: configService.get<string>('JWT_SECRET'), |
||
20 | }); |
||
21 | } |
||
22 | |||
23 | async validate(payload: JwtPayload) { |
||
24 | // this is not stateless, as we query the db. This can be refactored to be stateless, the token contains all user data. |
||
25 | 30 | const user = await this.authService.validateUserById(payload.sub); |
|
26 | 30 | if (!user) { |
|
27 | 2 | throw new UnauthorizedException(); |
|
28 | } |
||
29 | 28 | return user; |
|
30 | } |
||
32 |